<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" type="image/jpeg" sizes="80x80" href="imgs/logo.jpg" />
    <title>Login Page</title>
    <style type="text/css">
        .form-bg {
            background: #00b4ef;
        }

        .form-horizontal {
            background: #fff;
            padding-bottom: 40px;
            border-radius: 15px;
            text-align: center;
        }

        .form-horizontal .heading {
            display: block;
            font-size: 35px;
            font-weight: 700;
            padding: 35px 0;
            border-bottom: 1px solid #f0f0f0;
            margin-bottom: 30px;
        }

        .form-horizontal .form-group {
            padding: 0 40px;
            margin: 0 0 25px 0;
            position: relative;
        }

        .form-horizontal .form-control {
            background: #f0f0f0;
            border: none;
            border-radius: 20px;
            box-shadow: none;
            padding: 0 20px 0 45px;
            height: 40px;
            transition: all 0.3s ease 0s;
        }

        .form-horizontal .form-control:focus {
            background: #e0e0e0;
            box-shadow: none;
            outline: 0 none;
        }

        .form-horizontal .form-group i {
            position: absolute;
            top: 12px;
            left: 60px;
            font-size: 17px;
            color: #c8c8c8;
            transition: all 0.5s ease 0s;
        }

        .form-horizontal .form-control:focus+i {
            color: #00b4ef;
        }

        .form-horizontal .fa-question-circle {
            display: inline-block;
            position: absolute;
            top: 12px;
            right: 60px;
            font-size: 20px;
            color: #808080;
            transition: all 0.5s ease 0s;
        }

        .form-horizontal .fa-question-circle:hover {
            color: #000;
        }

        .form-horizontal .main-checkbox {
            float: left;
            width: 20px;
            height: 20px;
            background: #11a3fc;
            border-radius: 50%;
            position: relative;
            margin: 5px 0 0 5px;
            border: 1px solid #11a3fc;
        }

        .form-horizontal .main-checkbox label {
            width: 20px;
            height: 20px;
            position: absolute;
            top: 0;
            left: 0;
            cursor: pointer;
        }

        .form-horizontal .main-checkbox label:after {
            content: "";
            width: 10px;
            height: 5px;
            position: absolute;
            top: 5px;
            left: 4px;
            border: 3px solid #fff;
            border-top: none;
            border-right: none;
            background: transparent;
            opacity: 0;
            -webkit-transform: rotate(-45deg);
            transform: rotate(-45deg);
        }

        .form-horizontal .main-checkbox input[type=checkbox] {
            visibility: hidden;
        }

        .form-horizontal .main-checkbox input[type=checkbox]:checked+label:after {
            opacity: 1;
        }

        .form-horizontal .text {
            float: left;
            margin-left: 7px;
            line-height: 20px;
            padding-top: 5px;
            text-transform: capitalize;
        }

        .form-horizontal .btn {
            float: right;
            font-size: 14px;
            color: #fff;
            background: #00b4ef;
            border-radius: 30px;
            padding: 10px 25px;
            border: none;
            text-transform: capitalize;
            transition: all 0.5s ease 0s;
        }

        @media only screen and (max-width: 479px) {
            .form-horizontal .form-group {
                padding: 0 25px;
            }

            .form-horizontal .form-group i {
                left: 45px;
            }

            .form-horizontal .btn {
                padding: 10px 20px;
            }
        }
    </style>
</head>

<body class="form-bg">

    <div id="root">

        <div class="container">
            <div class="row justify-content-center mt-5">
                <div class="col-md-offset-3 col-md-6">
                    <form @submit.prevent="oauth" class="form-horizontal pb-5">
                        <span class="heading">
                            <span>Sign in</span>
                            <i class="fa fa-sign-in mr-2"></i>
                        </span>
                        <div class="form-group">
                            <input type="text" v-model="username" class="form-control" id="inputEmail3"
                                placeholder="Username">
                            <i class="fa fa-user"></i>
                        </div>
                        <div class="form-group help">
                            <input type="password" v-model="password" class="form-control" id="inputPassword3"
                                placeholder="Password">
                            <i class="fa fa-lock"></i>
                        </div>
                        <div v-show="loginErr" class="alert alert-danger m-2">用户名或密码错误</div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-outline-primary">登 录</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
    <link rel="stylesheet" href="/cdn/bootstrap/bootstrap.min.css" />
    <link href="/cdn/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="/cdn/vue/vue.min.js"></script>
    <script src="/cdn/axios.min.js"></script>
    <script>
        const config = {
            client_id: "31d911f47eda3cad2d7e0b6923cbe4082d633cc81e79826804d574d850c9972a",
            client_secret: "a454f37510ca5483d4f004f02d016405a0a70e819d3ef14c8db1bf980ebfc39f",
            username: "yanhui2018",
            project: "webdata"
        };

        const storage_login = "wyd-login-state";
        new Vue({
            el: "#root",
            data() {
                return { username: "", password: "", loginErr: false, loginErrMsg: "" }
            },
            methods: {

                oauth() {
                    if (this.username.trim() === "" || this.password.trim() === "") {
                        return false;
                    }

                    axios.post('https://gitee.com/oauth/token', {
                        grant_type: "password",
                        username: this.username,
                        password: this.password,
                        client_id: config.client_id,
                        client_secret: config.client_secret,
                        scope: "projects",
                    }).then(response => {
                        if (response.status === 200) {
                            localStorage.setItem(storage_login, JSON.stringify(response.data));
                            this.navPage();
                        }
                    }).catch((err) => {
                        this.loginErrMsg = err;
                        this.loginErr = true;
                    });

                    return false;
                },

                loginValid() {
                    let loginStorageData = localStorage.getItem(storage_login);
                    if (loginStorageData) {
                        let { created_at, expires_in, refresh_token } = JSON.parse(loginStorageData);
                        if (Math.floor(Date.now() / 1000) - created_at > expires_in) {
                            axios.post(`https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token=${refresh_token}`).then(response => {
                                if (response.status === 200) {
                                    localStorage.setItem(storage_login, JSON.stringify(response.data));
                                    this.navPage();
                                }
                            });
                        } else {
                            this.navPage();
                        }
                    }
                },

                navPage() {
                    window.location.href = "/pages/yanhui.html";
                }
            },
            mounted() {
                this.loginValid();
            }
        })
    </script>
</body>

</html>